package frame.options.simulation.results.physicalNetworks.numerical;

import java.util.Iterator;
import java.util.Map.Entry;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import model.components.links.link.BandwidthBackupFlag;
import model.components.links.link.PhysicalLink;
import model.components.links.link.VirtualLink;
import model.components.networks.network.PhysicalNetwork;
import model.components.nodes.node.VirtualNode;
import model.components.nodes.node.PhysicalNode;

@SuppressWarnings("serial")
public class PhysicalNetworkTable extends JTable
{
	private PhysicalNetwork physicalNetwork;
	public PhysicalNetwork getPhysicalNetwork(){ return physicalNetwork; }
	public void setPhysicalNetwork( PhysicalNetwork physicalNetwork ){ this.physicalNetwork = physicalNetwork; }

	public PhysicalNetworkTable( PhysicalNetwork physicalNetwork )
	{	
		this.setPhysicalNetwork( physicalNetwork );
		
		this.setModel( this.defaultTableModel() );
		this.setEnabled( false );
	}
	
	private DefaultTableModel defaultTableModel()
	{
		int leasedCPU = 0;
		int leasedMemory = 0;
		int leasedDiskSpace = 0;
		
		int cpuUnitTimeUnitPrice = this.getPhysicalNetwork().getCpuUnitTimeUnitPrice();
		int memoryUnitTimeUnitPrice = this.getPhysicalNetwork().getMemoryUnitTimeUnitPrice();
		int diskSpaceUnitTimeUnitPrice = this.getPhysicalNetwork().getDiskSpaceUnitTimeUnitPrice();
		
		int cpuRevenue = 0;
		int memoryRevenue = 0;
		int diskSpaceRevenue = 0;
				
		for( PhysicalNode physicalNode: this.getPhysicalNetwork().getPhysicalNodes() )
			if( physicalNode.getVirtualNodes().size() != 0 )
				for( VirtualNode virtualNode: physicalNode.getVirtualNodes() )
					if( virtualNode.getStartingDate() == this.getPhysicalNetwork().getTimeStamp() )
					{
						leasedCPU += virtualNode.getCpu();
						leasedMemory += virtualNode.getMemory();
						leasedDiskSpace += virtualNode.getDiskSpace();
						
						cpuRevenue += virtualNode.getCpu() * cpuUnitTimeUnitPrice * virtualNode.getLifeTime();
						memoryRevenue += virtualNode.getMemory() * memoryUnitTimeUnitPrice * virtualNode.getLifeTime();
						diskSpaceRevenue += virtualNode.getDiskSpace() * diskSpaceUnitTimeUnitPrice * virtualNode.getLifeTime();
					}
		
		int leasedBandwidth = 0;
		int bandwidthRevenue = 0;
		int bandwidthUnitTimeUnitPrice = this.getPhysicalNetwork().getBandwidthUnitTimeUnitPrice();
		
		for( PhysicalLink physicalLink: this.getPhysicalNetwork().getPhysicalLinks() )
		{
			Iterator< Entry< VirtualLink, BandwidthBackupFlag > > iterator = physicalLink.getVirtualLinksBandwidthsBackupFlags().entrySet().iterator();
			
			while( iterator.hasNext() )
			{
				Entry< VirtualLink, BandwidthBackupFlag > entry = iterator.next();
				VirtualLink virtualLink = entry.getKey();
				int bandwidth = entry.getValue().getBandwidth();
					
				if( virtualLink.getStartingDate() == this.getPhysicalNetwork().getTimeStamp() )
				{
					leasedBandwidth += bandwidth;
					bandwidthRevenue += bandwidth * bandwidthUnitTimeUnitPrice * virtualLink.getLifeTime();
				}
			}
		}
		
		int networkRevenue = cpuRevenue + memoryRevenue + diskSpaceRevenue + bandwidthRevenue;
		this.getPhysicalNetwork().setNetworkRevenue( networkRevenue );
		
		DefaultTableModel defaultTableModel = new DefaultTableModel( new String[]{ "<html><b>Resource</b></html>", "<html><b>Leased</b></html>", "<html><b>Unit Price</b>", "<html><b>Revenue</b></html>" }, 0 );
		defaultTableModel.addRow( new Object[]{ "<html><b>CPU</b></html>", leasedCPU, cpuUnitTimeUnitPrice, cpuRevenue } );
		defaultTableModel.addRow( new Object[]{ "<html><b>Memory</b></html>", leasedMemory, memoryUnitTimeUnitPrice, memoryRevenue } );
		defaultTableModel.addRow( new Object[]{ "<html><b>Disk Space</b></html>", leasedDiskSpace, diskSpaceUnitTimeUnitPrice, diskSpaceRevenue } );
		defaultTableModel.addRow( new Object[]{ "<html><b>Bandwidth</b></html>", leasedBandwidth, bandwidthUnitTimeUnitPrice, bandwidthRevenue } );
		defaultTableModel.addRow( new Object[]{ "<html><b>Total</b></html>", "", "", "<html><b>" + networkRevenue + "</b></html>" } );
		
		return defaultTableModel;
	}
	
}
